package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据保存到文件中
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String score = req.getParameter("score");

        // 将数据用字符输出流写入指定地点
        BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\stu.txt",true));
        bw.write(name + "," + age + "," + score);
        bw.newLine();
        bw.close();

        // 响应到客户浏览器
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("添加成功，1秒后跳转到首页");
        resp.setHeader("Refresh","1;URL=/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
